package com.wu.util.spider.service;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.net.URI;

/**
 * 爬虫分析的模版抽象类
 *
 * @author xwu6
 */
public abstract class BaseSpiderWorkerService {

    /**
     * 用户代理
     */
    private static final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36";
    /**
     * 请求超时时间
     */
    private static final Integer TIME_OUT = 5000;

    private static final HttpGet REQUEST_GET = new HttpGet();

    /**
     * HttpClient实例
     */
    protected static final CloseableHttpClient HTTP_CLIENT = HttpClients.createDefault();
    protected static RequestConfig REQUEST_CONFIG = null;

    /**
     * 返回httpclient请求方法
     * @return HttpRequestBase
     */
    protected HttpRequestBase getRequestMethod(URI url) {
        REQUEST_GET.setConfig(REQUEST_CONFIG);
        REQUEST_GET.setHeader("User-Agent", USER_AGENT);
        REQUEST_GET.setURI(url);
        return REQUEST_GET;
    }

    /**
     * 初始化爬虫配置
     */
    public void initService() {
        REQUEST_CONFIG = RequestConfig.custom().setConnectTimeout(TIME_OUT)
                .setConnectionRequestTimeout(TIME_OUT).build();
    }

    /**
     * 启动分析服务,需要实现类重写
     */
    public abstract void startService();

}
